package com.chinatgl.latte.net;

import com.chinatgl.latte.app.ConfigKeys;
import com.chinatgl.latte.app.Latte;
import com.chinatgl.latte.net.rx.RxRestService;

import java.util.ArrayList;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * 文件描述: 初始化网络请求
 * 作者:  杨伟乔
 * 时间: 2017/8/7 0007.
 * 版本号: 1.0
 */
@SuppressWarnings("all")
public class RestCreator {


    public static final class ParamsHolder {
        public static final WeakHashMap<String, Object> PARAMS
                = new WeakHashMap<>();
    }

    public static WeakHashMap<String, Object> getParams()

    {
        return ParamsHolder.PARAMS;
    }



    /**
     * 构建全局Retrofit客户端
     */
    private static final class RetrofitHolder {
        private static final String BASE_URL = (String) Latte.getConfigurations().get(ConfigKeys.API_HOST);
        //使用建造者模式初始化
        private static final Retrofit RETROFIT_CLIENT = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(OKHttpholder.OK_HTTP_CLIENT)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    //初始化okhttpclient
    private static final class OKHttpholder {
        //超时时间
        private static final int TIME_OUT = 15;
        //拦截器
        private static final ArrayList<Interceptor> INTERCEPTORS = Latte.getConfiguration(ConfigKeys.INTERCEPTOR);
        //
        private static final OkHttpClient.Builder BUILDER = new OkHttpClient.Builder();

        //循环把拦截器传入到okhhtp中
        private static OkHttpClient.Builder addInterceptor() {
            if (INTERCEPTORS != null && !INTERCEPTORS.isEmpty()) {
                for (Interceptor interceptor : INTERCEPTORS) {
                    //添加拦截器
                    BUILDER.addInterceptor(interceptor);
                }
            }
            return BUILDER;
        }

        private static final OkHttpClient OK_HTTP_CLIENT = addInterceptor()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 获取RestService
     */
    public static RestService getRestService() {
        return RestServiceHolder.REST_SERVICE;
    }

    /**
     * service 接口
     */
    private static final class RestServiceHolder {
        private static final RestService REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT
                .create(RestService.class);
    }



    /**
     * Service接口
     */
    private static final class RxRestServiceHolder {

        private static final RxRestService REST_SERVICE =
                RetrofitHolder.RETROFIT_CLIENT.create(RxRestService.class);
    }

    public static RxRestService getRxRestService() {
        return RxRestServiceHolder.REST_SERVICE;
    }
}
